package menu.lt.pers.menu;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import menu.lt.pers.menu.Bean.UserBean;
import menu.lt.pers.menu.dao.UserDao;

public class LoginActivity extends FragmentActivity {
    private TextView tvTitle;
    private EditText etPhone;
    private EditText etPwd;
    private Button btnLogin;
    private Button btnRegister;
    private CheckBox isRemember;
    private SharedPreferences preference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        setListener();
    }

    private void setListener() {
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etPhone.getText().toString();
                String pwd = etPwd.getText().toString();
                UserBean user = new UserBean();
                user.setPhone(phone);
                UserDao userDao = new UserDao(LoginActivity.this);
                Cursor cursor = userDao.findByPhone(user);
                if(cursor.getCount()>0) {
                    cursor.moveToFirst();
                    if (cursor.getString(4).equals(pwd)){
                        int id = cursor.getInt(0);
                        String username = cursor.getString(1);
                        int isdelete = cursor.getInt(2);
                        String userhead = cursor.getString(3);
                        user.setId(id);
                        user.setUsername(username);
                        user.setUserhead(userhead);
                        user.setIsdelete(isdelete);
                        user.setPwd(pwd);
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        intent.putExtra("user",user);
                        String msg = getResources().getString(R.string.login_success) + "亲~~~";
                        Toast.makeText(LoginActivity.this,msg,
                                Toast.LENGTH_SHORT).show();
                        SharedPreferences.Editor editor = preference.edit();
                        if (isRemember.isChecked()) {//记住账号与密码
                            editor.putBoolean("isRemember", true);
                            editor.putString("phone", user.getPhone());
                            editor.putString("pwd", user.getPwd());
                        } else {
                            editor.clear();
                        }
                        editor.apply();
                        startActivity(intent);

                    } else {
                        Toast.makeText(LoginActivity.this,
                                getResources().getString(R.string.login_error_msg1),
                                Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(LoginActivity.this,
                            getResources().getString(R.string.login_error_msg2),
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    private void init() {
        tvTitle =  findViewById(R.id.textView);
        etPhone =  findViewById(R.id.et_phone);
        etPwd =  findViewById(R.id.et_pwd);
        btnLogin =  findViewById(R.id.btn_login);
        btnRegister =  findViewById(R.id.btn_register);
        isRemember = findViewById(R.id.isRemember);

        preference = PreferenceManager.getDefaultSharedPreferences(this);
    }

}
